package com.example.userlogincenter.userCenter.service.impl;


import api.apiservice.apiopenplat.entity.ApiInfo;
import api.apiservice.apiopenplat.service.IApiInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.userlogincenter.userCenter.entity.ApikeyInfo;
import com.example.userlogincenter.userCenter.entity.CenterUser;
import com.example.userlogincenter.userCenter.mapper.ApiMemberMapper;
import com.example.userlogincenter.userCenter.mapper.ApikeyInfoMapper;
import com.example.userlogincenter.userCenter.pojo.memeberInfo;
import com.example.userlogincenter.userCenter.service.ApiMemberService;
import com.example.userlogincenter.userCenter.service.ApikeyInfoService;
import com.example.userlogincenter.userCenter.service.ICenterUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.example.userlogincenter.userCenter.entity.ApiMember;

import java.util.ArrayList;
import java.util.List;

/**
* @author ASUS
* @description 针对表【api_member(描述用户与API的对应关系)】的数据库操作Service实现
* @createDate 2023-04-02 22:40:31
*/
@Service
public class ApiMemberServiceImpl extends ServiceImpl<ApiMemberMapper, ApiMember>
    implements ApiMemberService {

    @DubboReference
    private IApiInfoService apiInfoService;

    @Autowired
    private  ApikeyInfoMapper apikeyInfoMapper;

    @Autowired
    private ICenterUserService centerUserService;

    @Override
    public memeberInfo getMemberInfo(Long mid) {
        memeberInfo memeberInfo = new memeberInfo();
        //查询用户使用api情况
        QueryWrapper<ApiMember> queryWrapper = new QueryWrapper();
        queryWrapper.eq("memberId",mid);
        List<ApiMember> list = this.baseMapper.selectList(queryWrapper);
        //查询用户apikey
        QueryWrapper<ApikeyInfo> queryWrapperkey = new QueryWrapper<>();
        queryWrapperkey.eq("ak_memberid",mid);
        List<ApikeyInfo> apikeyInfos = apikeyInfoMapper.selectList(queryWrapperkey);
        List<ApiInfo> apiList = new ArrayList<>();
        List<ApikeyInfo> apikeyList = new ArrayList<>();
        list.forEach(v -> {
            ApiInfo apiInfo = apiInfoService.searchApiById(v.getApiId());
            apiList.add(apiInfo);
        });
        apikeyInfos.forEach(v -> {
            apikeyList.add(v);
        });
        memeberInfo.setApiList(apiList);
        memeberInfo.setKeyList(apikeyList);
        //查询用户信息
        CenterUser user = centerUserService.getUserById(mid);
        memeberInfo.setHeadimage(user.getHeadimage());
        memeberInfo.setNickname(user.getNickname());
        memeberInfo.setPhonenumber(user.getPhonenumber());
        return memeberInfo;
    }
}




